from rest_framework.permissions import IsAdminUser
from rest_framework.views import APIView
from apps.users.models import User
import datetime
from rest_framework.views import Response
from rest_framework.views import status
from rest_framework.decorators import api_view,permission_classes
from apps.wthg_admin import utils

class UserDailyActiveCountView(APIView):
    """日活跃用户统计"""

    def get(self,request):
        utils.private_authorization(self)
        now_date = datetime.date.today()
        count=User.objects.filter(last_login__gte=now_date).count()

        return Response({'count':count,'date':now_date},status=status.HTTP_200_OK)


class UserDailyOrderCountView(APIView):
    """日下单用户量统计"""
    def get(self,request):
        utils.private_authorization(self)
        date = datetime.date.today()
        count=User.objects.filter(orderinfo__create_time__gte=date).count()

        return Response({ "count": count,"date": date},status=status.HTTP_200_OK)

class TotalUserStatisticsView(APIView):
    """用户总量统计"""
    def get(self,request):
        utils.private_authorization(self)
        date = datetime.date.today()
        count=User.objects.filter(is_staff=0).count()

        return Response({ "count": count,"date": date},status=status.HTTP_200_OK)

class  DailyUserStatisticsView(APIView):
    """日增用户统计"""

    def get(self,request):
        utils.private_authorization(self)
        date = datetime.date.today()
        count = User.objects.filter(date_joined__gte=date,is_staff=0).count()
        return Response({"count": count, "date": date}, status=status.HTTP_200_OK)

class MonthlyUserStatistics(APIView):
    """月增用户统计"""

    def get(self,request):
        utils.private_authorization(self)
        # 获得今天的日期
        today=datetime.date.today()
        # 往后推30天
        month=today-datetime.timedelta(days =30)
        #保存天数据的列表
        days=[]
        # 遍历这30天循环查询
        for x in range(31):
            index_date  = month + datetime.timedelta(days=x) #今天
            cur_date =month+datetime.timedelta(days=x+1) #明天
            #查询
            count=User.objects.filter(date_joined__gte=index_date,date_joined__lt=cur_date).count()
            #保存
            days.append({'count':count,'date':index_date})
        # 返回数据
        return Response(days,status=status.HTTP_200_OK)

from apps.goods.models import GoodsCategory,GoodsVisitCount
class ProductVisitsView(APIView):
    """商品类型访问量"""
    def get(self,request):
        utils.private_authorization(self)
        #statistical/goods_day_views/
        #{category:'商品类别','count':总数}
        new_date = datetime.date.today()
        t1=GoodsCategory.objects.filter(parent=None)
        datas=[{'category':x.name,'count':0} for x in t1] #创建返回的指定格式数据

        goods_list=GoodsVisitCount.objects.filter(date=new_date) #获得今天所有的商品类型的点击率

        for category2 in goods_list: #遍历赋值格式化
            QS=category2.category.parent.parent #拿到商品类型
            for i in range(len(datas)): #遍历判断 赋值
                if QS.name == datas[i]['category']:
                    datas[i]['count']=datas[i]['count']+int(category2.count)

        return Response(datas,status=status.HTTP_200_OK)









